package com.example.demo.controller;

import com.example.demo.base.*;
import com.example.demo.entity.HouseInfo;
import com.example.demo.service.HouseInfoService;
import com.example.demo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Validated
@RequestMapping({"/manage/"})
public class HouseInfoController{

    @Autowired
    private HouseInfoService houseInfoService;


    @RequestMapping(value = "/house/list")
    public BaseResponse getUserList(
            @RequestParam(required = false, value = "floorId") Integer floorId,
            @RequestParam(required = false, value = "unitId") Integer unitId,
            @RequestParam(required = false, value = "roomId") String roomId,
            @RequestParam(required = false, value = "state") Integer state,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request) {

        QueryBuilder queryBuilder = new QueryBuilder();
        if(floorId!=null){
            queryBuilder.addFilter(Filter.eq("floor_id",floorId));
        }
        if(unitId!=null){
            queryBuilder.addFilter(Filter.eq("unit_id",unitId));
        }
        if(StringUtils.isNotBlank(roomId)){
            queryBuilder.addFilter(Filter.eq("room_id",roomId));
        }
        if(state!=2){
            queryBuilder.addFilter(Filter.eq("state",state));
        }
        Pageable pageable = houseInfoService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        response.setRows(pageable.getRecords());
        response.setTotal(new Long(pageable.getTotal()).intValue());

        return response;
    }

    @RequestMapping(value = "/house/update")
    public BaseResponse updateUser(
            @RequestParam(required = false, value = "id") Long id,
            @RequestParam(required = false, value = "state") Integer state,
            HttpServletRequest request){
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setState(state);
        UpdateBuilder updateBuilder = new UpdateBuilder(houseInfo);
        updateBuilder.addFilter(Filter.eq("id",id));
        houseInfoService.updateEntity(updateBuilder);

        return BaseResponse.success();
    }
}
